/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;
	import flash.text.*;
	import flash.utils.getTimer;
	
	/**
	 * STATS VIEW CLASS
	 * 
	 * places a little rectangle in the upper left hand of your screen.  
	 * stats view displays total memory usage and current frames per second
	 *  
	 * @author thoms
	 * 
	 */
	public class StatsView extends Sprite
	{
		private var nowTime:Number = 0;
		private var lastTime:Number = 0;
		private var statsFormat:TextFormat
		private var punyText:TextField;
		private var fpsInfoTextField:TextField;
		private var memInfoTextField:TextField
		
		/**
		 * public constructor 
		 * 
		 * creates background and textfields, adds them to the stage
		 * adds EventListener for enter frame
		 * 
		 */
		public function StatsView()
		{
			drawBackGround();
			
			statsFormat = new TextFormat("Arial", 12, 0xFFFFFF, false, false, false);
			
			punyText = new TextField();
			punyText.y = 0;
			punyText.autoSize = TextFieldAutoSize.LEFT;
			punyText.defaultTextFormat = statsFormat;
			punyText.text = "PUNY STATS";
			addChild(punyText);
			
			
			fpsInfoTextField = new TextField();
			fpsInfoTextField.y = 16;
			fpsInfoTextField.autoSize = TextFieldAutoSize.LEFT;
			fpsInfoTextField.defaultTextFormat = statsFormat;
			addChild(fpsInfoTextField);
			
			memInfoTextField = new TextField();
			memInfoTextField.y = 32;
			memInfoTextField.autoSize = TextFieldAutoSize.LEFT;
			memInfoTextField.defaultTextFormat = statsFormat;
			addChild(memInfoTextField);
			
			addEventListener(Event.ENTER_FRAME, updateStats);
		}
		
		/**
		 *Draws the black background behind the text
		 * 
		 */
		private function drawBackGround():void
		{
			this.graphics.beginFill(0x000000, .8);
			this.graphics.drawRoundRect(0, 0, 100, 50, 5);
			this.graphics.endFill();
		}
		
		/**
		 * 
		 * @param event				Enterframe Event updates Memory usage and frames per second.
		 * 
		 */
		private function updateStats(event:Event):void
		{
			memInfoTextField.text = "MEM : "+(System.totalMemory/1024/1024).toFixed(2) + "MB";
			
			nowTime = getTimer();
			var nowfps:Number = Math.floor((1000/(nowTime - lastTime)));
			lastTime = nowTime;
			
			fpsInfoTextField.text = "FPS : "+String(nowfps);
		}
		
	}
}